package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.service.ICommonService;
import com.boss.salary.service.*;
import com.boss.salary.vo.AgencyVerifyParamVo;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/agencySalaryBatchController")
public class AgencySalaryBatchController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AgencySalaryController.class);
    public final static String Executing = "Executing";

    @Autowired
    private IAgencySalaryService service;
    @Autowired
    private IAgencyInfoService agencyInfoService;
    @Autowired
    private IFormulaManageService formulaManageService;

    @Autowired
    private IGenerateSalaryDataService iGenerateSalaryDataService;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    @Autowired
    private IRedisService redisService;

    //维护工资信息批量审核
    @PostMapping("/submitUnitSalaryReport")
    public ApiResultDTO submitUnitSalaryReport(@RequestHeader("X-Token") String tokenid,
                                               @RequestBody List<AgencyVerifyParamVo> agencyVerifyParamVo) throws Exception {
        UserDTO userDTO = getUser(tokenid);
        try {
            Map<String, String> requestMap = new HashMap<>();
            Object re = redisService.get(userDTO.getProvince() + "submitLock");
            //查询缓存中该区划是否有在执行的生成操作
            if (!SysUtils.isNullOrEmpty(re)) {
                String uuid = "2021-" + userDTO.getProvince() + "-menuId";
                requestMap.put("guid", uuid);
                requestMap.put("已执行批量操作，请稍后刷新查看", Executing);
                return ApiResultDTO.success(requestMap);
            }
            redisService.put(userDTO.getProvince() + "submitLock", 1);//添加执行控制，避免重复操作
            Map<String,String> map =service.batchSubmitUnitSalaryReport(agencyVerifyParamVo,userDTO,tokenid);
            return ApiResultDTO.success(map);
        } catch (Exception e) {
            redisService.del(userDTO.getProvince() + "submitLock");
            logger.error("送审发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "送审失败");
        }
    }


    /**
     * 查看维护工资信息批量审核进度
     * @param uuid
     * @return
     */
    @GetMapping("/getSubmitPercent")
    public ApiResultDTO getSubmitPercent(@RequestParam(value = "uuid") String uuid,@RequestHeader("X-Token") String tokenid){
        try {
            UserDTO userDto = getUser(tokenid);
            Object re = redisService.get(uuid);

            if ("S".equals(re) || (re != null && re.toString().startsWith("F"))|| (re != null && re.toString().startsWith("W"))) {
                redisService.del(uuid);
                Object generateSalaryState = redisService.get(uuid);
                if(generateSalaryState!=null) {
                    redisService.del(uuid);
                }
            }
            return ApiResultDTO.success(re);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("批量报送失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }


    //审核报财政批量审核
    @PostMapping("/AuditUnitSalaryReport")
    public ApiResultDTO AuditUnitSalaryReport(@RequestHeader("X-Token") String tokenid,
                                               @RequestBody List<AgencyVerifyParamVo> agencyVerifyParamVo) throws Exception {
        UserDTO userDTO = getUser(tokenid);
        try {
            Map<String, String> requestMap = new HashMap<>();
            Object re = redisService.get(userDTO.getProvince() + "batchLock");
            //查询缓存中该区划是否有在执行的生成操作
            if (!SysUtils.isNullOrEmpty(re)) {
                String uuid = "2021-" + userDTO.getProvince() + "-menuId";
                requestMap.put("guid", uuid);
                requestMap.put("已执行批量操作，请稍后刷新查看", Executing);
                return ApiResultDTO.success(requestMap);
            }
            redisService.put(userDTO.getProvince() + "batchLock", 1);//添加执行控制，避免重复操作
            Map<String,String> map =service.batchAuditUnitSalaryReport(agencyVerifyParamVo,userDTO,tokenid);
            return ApiResultDTO.success(map);
        } catch (Exception e) {
            redisService.del(userDTO.getProvince() + "batchLock");
            logger.error("送审发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "送审失败");
        }
    }


    /**
     * 查看审核报财政批量审核进度
     */
    @GetMapping("/getAuditPercent")
    public ApiResultDTO getAuditPercent(@RequestParam(value = "uuid") String uuid,@RequestHeader("X-Token") String tokenid){
        try {
            UserDTO userDto = getUser(tokenid);
            Object re = redisService.get(uuid);

            if ("S".equals(re) || (re != null && re.toString().startsWith("F"))) {
                redisService.del(uuid);
                Object generateSalaryState = redisService.get(uuid);
                if(generateSalaryState!=null) {
                    redisService.del(uuid);
                }
            }
            return ApiResultDTO.success(re);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("批量审核失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }



//单岗批量审核
    @PostMapping("/AuditUnitSalaryReportOne")
    public ApiResultDTO AuditUnitSalaryReportOne(@RequestHeader("X-Token") String tokenid,
                                              @RequestBody List<AgencyVerifyParamVo> agencyVerifyParamVo) throws Exception {
        UserDTO userDTO = getUser(tokenid);
        try {
            Map<String, String> requestMap = new HashMap<>();
            Object re = redisService.get(userDTO.getProvince() + "submitLock");
            //查询缓存中该区划是否有在执行的生成操作
            if (!SysUtils.isNullOrEmpty(re)) {
                String uuid = "2021-" + userDTO.getProvince() + "-menuId";
                requestMap.put("guid", uuid);
                requestMap.put("已执行批量操作，请稍后刷新查看", Executing);
                return ApiResultDTO.success(requestMap);
            }
            redisService.put(userDTO.getProvince() + "submitLock", 1);//添加执行控制，避免重复操作
            Map<String,String> map =service.batchAuditUnitSalaryReportOne(agencyVerifyParamVo,userDTO,tokenid);
            return ApiResultDTO.success(map);
        } catch (Exception e) {
            redisService.del(userDTO.getProvince() + "submitLock");
            logger.error("送审发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "送审失败");
        }
    }







//查询单岗批量审核进度
    @GetMapping("/getAuditPercentOne")
    public ApiResultDTO getAuditPercentOne(@RequestParam(value = "uuid") String uuid,@RequestHeader("X-Token") String tokenid){
        try {
            UserDTO userDto = getUser(tokenid);
            Object re = redisService.get(uuid);

            if ("S".equals(re) || (re != null && re.toString().startsWith("F"))) {
                redisService.del(uuid);
                Object generateSalaryState = redisService.get(uuid);
                if(generateSalaryState!=null) {
                    redisService.del(uuid);
                }
            }
            return ApiResultDTO.success(re);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("批量审核失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }






}
